<?php

namespace App\Console\Commands;

use App\Advertiser;
use App\AdvertiserImg;
use App\Jobs\PullImage;
use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Log;

class SyncImage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sync:image';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '同步图片资源';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 获取当前时间
        $now = Carbon::now();
        // 获取前一个月的时间
        $previousMonth = $now->subHours(3);
//        $previousMonth = $now->subDays(3);
        // 获取前一个月的时间戳
        $timestamp = $previousMonth->timestamp;
        Log::info("定时任务同步图片资源开始", [
            'timestamp' => $timestamp
        ]);

        Advertiser::query()->where([
            'system_status' => 'CUSTOMER_STATUS_NORMAL'
        ])->whereIn('corporation_name', [
            "厦门恒权商贸有限公司","厦门领翱博域网络科技有限公司","厦门卓尔不群生物科技有限公司","深圳市海哲科技有限公司","泉州市栩栩如生生物科技有限公司","厦门奕帆星火贸易有限公司","厦门市海沧区昕昕玥电子商务经营部","厦门领翱云图网络科技有限公司","故里茶乡(漳平)茶业有限公司"
            ,'厦门伊隐文电子商务有限公司','福州伊隐文网络科技有限公司','福建省德化盛世华瑞商贸有限公司'
        ])->chunkById(100, function ($advertiserList) use ($timestamp) {
            $advertiserList = $advertiserList->toArray();

            foreach ($advertiserList as $advertiser) {
                PullImage::dispatch($advertiser['account_id'], $timestamp);
            }

        });

        return true;
    }

}
